<?php
 

/* Check if this is a valid include */
if (!defined('IN_SCRIPT')) {die($hdlang['attempt']);}

$sql = "SELECT * FROM `hd_requests` WHERE ";

if ($_GET['archive']) {$archive=1;$sql .= "`archive`='1' AND ";}
else {$archive=0;}

$sql .= hd_myResidences();

/* Get all the SQL sorting preferences */
if (!isset($_GET['status'])) {$status=6; $sql .= " AND (`status`='0' OR `status`='1' OR `status`='2') ";}
else
{
    $status = hd_isNumber($_GET['status']);
    if ($status==5) {$sql .= " AND (`status`='0' OR `status`='1') ";}
    elseif ($status==6) {$sql .= " AND (`status`='0' OR `status`='1' OR `status`='2') ";}
    elseif ($status!=4) {$sql .= " AND `status`='$status' ";}
}

$residence = hd_isNumber($_GET['residence']) or $residence=0;
if ($residence) {$sql .= " AND `residence`='$residence' ";}
	 
$sql_copy=$sql;

$maxresults = hd_isNumber($_GET['limit']) or $maxresults=$hd_settings['max_listings'];
$thispage = hd_isNumber($_GET['page']) or $thispage=1;

$sort = hd_input($_GET['sort']) or $sort='priority';
$sql .= " ORDER BY `$sort` ";

if (isset($_GET['asc']) && $_GET['asc']==0) {$sql .= " DESC ";$asc=0;$asc_rev=1;}
else {$sql .= " ASC ";$asc=1;$asc_rev=0;}

/* This query string will be used to browse pages */
$query = "status=$status&sort=$sort&residence=$residence&asc=$asc&limit=$maxresults&archive=$archive&page=";
 
/* Get number of requests and page number */
$result = hd_dbQuery($sql_copy) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
$all = hd_dbNumRows($result);

$thispages = ceil($all/$maxresults) or $thispages=1;
if ($thispage > $thispages) {$thispage=$thispages;}
$limit_down = (($thispage*$maxresults)-$maxresults);

    $prev_page = ($thispage-1 <= 0) ? 0 : $thispage-1;
    $next_page = ($thispage+1 > $thispages) ? 0 : $thispage+1;

    echo '<form name="HPage"><p align="center">'.sprintf($hdlang['requests_on_pages'],$all,$thispages).' <select name="myHpage">';
    for ($i=1;$i<=$thispages;$i++) {
        echo '<option value="'.$i.'">'.$i.'</option>';
    }
    echo'</select> <input type="button" value="'.$hdlang['go'].'" onClick="Javascript:window.location=\'show_requests.php?'.$query.'\'+document.HPage.myHpage.value"><br />';

    if ($thispages > 7 && $prev_page) {
        echo '
        <a href="show_requests.php?'.$query.$i.'1">&lt;&lt;</a>
        &nbsp;
        <a href="show_requests.php?'.$query.$prev_page.'">&lt;</a>
        &nbsp;
        ';
    }

    for ($i=1; $i<=$thispages; $i++) {
        if ($i <= ($thispage+5) && $i >= ($thispage-5)) {
           if($i == $thispage) {echo ' <b>'.$i.'</b> ';}
           else {echo ' <a href="show_requests.php?'.$query.$i.'">'.$i.'</a> ';}
        }
    }

    if ($thispages > 7 && $next_page) {
        echo '
        &nbsp;
        <a href="show_requests.php?'.$query.$next_page.'">&gt;</a>
        &nbsp;
        <a href="show_requests.php?'.$query.$thispages.'">&gt;&gt;</a>
        ';
    }

    echo '</p></form>';


/* We have the full SQL query now, get requests */
$sql .= " LIMIT $limit_down,$maxresults ";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");

/* This query string will be used to order and reverse display */
$query = "status=$status&residence=$residence&asc=$asc_rev&limit=$maxresults&page=$thispage&archive=$archive&sort=";

/* Print the table with requests */
$random=rand(10000,99999);
?>

<form name="form1" action="delete_requests.php" method="POST">

<div align="center">
<center>
<table border="0" width="750" cellspacing="1" cellpadding="3" class="white">
<tr>
<td class="admin_white">&nbsp;</td>
<td class="admin_white"><a href="show_requests.php?<?php echo $query; ?>requestid"><?php echo $hdlang['trackID']; ?></a></td>
<td class="admin_white" align="center"><a href="show_requests.php?<?php echo $query; ?>lastchange"><?php echo $hdlang['last_update']; ?></a></td>
<td class="admin_white"><a href="show_requests.php?<?php echo $query; ?>name"><?php echo $hdlang['name']; ?></a></td>
<td class="admin_white"><a href="show_requests.php?<?php echo $query; ?>subject"><?php echo $hdlang['subject']; ?></a></td>
<td class="admin_white" align="center"><a href="show_requests.php?<?php echo $query; ?>status"><?php echo $hdlang['status']; ?></a></td>
<td class="admin_white" align="center"><a href="show_requests.php?<?php echo $query; ?>lastreplier"><?php echo $hdlang['last_replier']; ?></a></td>
<td class="admin_white" align="center"><a href="show_requests.php?<?php echo $query; ?>priority"><?php echo $hdlang['priority']; ?></a></td>
<td class="admin_white" align="center"><a href="show_requests.php?<?php echo $query; ?>archive"><?php echo $hdlang['archived']; ?></a></td>
</tr>

<?php
while ($request=hd_dbFetchAssoc($result))
{
    if ($i) {$color="admin_gray"; $i=0;}
    else {$color="admin_white"; $i=1;}

    switch ($request['status']) {
    case 0:
        $request['status']='<font class="open">'.$hdlang['open'].'</font>';
        break;
    case 1:
        $request['status']='<font class="waitingreply">'.$hdlang['wait_reply'].'</font>';
        break;
    case 2:
        $request['status']='<font class="replied">'.$hdlang['replied'].'</font>';
        break;
    default:
        $request['status']='<font class="resolved">'.$hdlang['closed'].'</font>';
    }

    switch ($request['priority']) {
    case 1:
        $request['priority']='<font class="important">'.$hdlang['high'].'</font>';
        break;
    case 2:
        $request['priority']='<font class="medium">'.$hdlang['medium'].'</font>';
        break;
    default:
        $request['priority']=$hdlang['low'];
    }

    $request['lastchange']=hd_formatDate($request['lastchange']);

    if ($request['lastreplier']=='1') {$request['lastreplier']=$hdlang['staff'];}
    else {$request['lastreplier']=$hdlang['Student'];}

    if ($request['archive']) {$request['archive']=$hdlang['yes'];}
    else {$request['archive']=$hdlang['no'];}

echo <<<EOC
<tr>
<td class="$color" align="center"><input type="checkbox" name="id[]" value="$request[id]"></td>
<td class="$color"><a href="admin_request.php?track=$request[requestid]&Refresh=$random">$request[requestid]</a></td>
<td class="$color" align="center">$request[lastchange]</td>
<td class="$color">$request[name]</td>
<td class="$color"><a href="admin_request.php?track=$request[requestid]&Refresh=$random">$request[subject]</a></td>
<td class="$color" align="center">$request[status]</td>
<td class="$color" align="center">$request[lastreplier]</td>
<td class="$color" align="center">$request[priority]</td>
<td class="$color" align="center">$request[archive]</td>
</tr>

EOC;
} // End while
?>
</table>
</center>
</div>

<p>&nbsp; <a href="Javascript:void(0)" onClick="Javascript:hd_changeAll()"><?php echo $hdlang['chg_all']; ?></a></p>

<p align="center"><select name="a">
<option value="delete" selected><?php echo $hdlang['del_selected']; ?></option>
<option value="close"><?php echo $hdlang['close_selected']; ?></option>
</select>
<input type="submit" value="<?php echo $hdlang['execute']; ?>" class="button"></p>

</form>
